package com.itao.config;


import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
import com.itao.annotation.DynamicHeadBean;
import com.itao.annotation.WriteHandlerBean;
import com.itao.handler.ExcelReturnValueHandler;
import jakarta.annotation.PostConstruct;
import lombok.AllArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sjt
 * @Date 2024/12/10 13:42
 */
@Configuration
@AllArgsConstructor
public class ExcelConfig {

    private RequestMappingHandlerAdapter requestMappingHandlerAdapter;

    @PostConstruct
    public void init() {
        List<HandlerMethodReturnValueHandler> returnValueHandlers =
                requestMappingHandlerAdapter.getReturnValueHandlers();
        List<HandlerMethodReturnValueHandler> newHandlers = new ArrayList<>();
        newHandlers.add(new ExcelReturnValueHandler());
        if (returnValueHandlers != null) {
            newHandlers.addAll(returnValueHandlers);
        }
        requestMappingHandlerAdapter.setReturnValueHandlers(newHandlers);
    }

    @Bean
    public DynamicHeadBean dynamicHead(){
        return () -> {
            List<List<String>> heads = new ArrayList<>();
            heads.add(List.of("姓名1"));
            heads.add(List.of("地址1"));
            return heads;
        };
    }

    @Bean
    public WriteHandlerBean writeHandlerBean(){
        return () -> {
            List<WriteHandler> writeHandlers = new ArrayList<>();
            //writeHandlers.add(new )
            writeHandlers.add(new SimpleColumnWidthStyleStrategy(20));
            return writeHandlers;
        };
    }
}
